package com.example.juyixia;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.method.DigitsKeyListener;
import android.util.ArrayMap;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TextView;

public class SignUpActivity extends Activity {
	private Context context;// 上下文
	private Button btn_confirm;// 确认按钮
	private TextView username;// 用户名
	private TextView userpwd;// 密码
	private TextView age;// 年龄
	private RadioButton male;// 性别男选择框
	private RadioButton female;// 女性
	private boolean checked = true;// 判断性别为男还是女 1男0女
	private Handler handler, handler2;

	private String issuccess;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_sign_up);
		initViews();
	}

	private void initViews() {
		// TODO Auto-generated method stub
		username = (TextView) findViewById(R.id.username);
		username.setKeyListener(DigitsKeyListener.getInstance("0123456789abcdefghigklmnopqrstuvwxyz"));
		userpwd = (TextView) findViewById(R.id.userpwd);
		// 设置密码只能输入字母和数字
		userpwd.setKeyListener(DigitsKeyListener.getInstance("0123456789abcdefghigklmnopqrstuvwxyz"));
		age = (TextView) findViewById(R.id.userage);
		// 设置年龄只能输入数字
		age.setKeyListener(DigitsKeyListener.getInstance("1234567890"));
		female = (RadioButton) findViewById(R.id.radioButton_female);
		male = (RadioButton) findViewById(R.id.radioButton_male);
		male.setChecked(true);
		male.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (male.isChecked() == true)
					female.setChecked(false);
				else
					male.setChecked(true);
				checked = true;
			}
		});
		female.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (female.isChecked() == true)
					male.setChecked(false);
				else
					female.setChecked(true);
				checked = false;
			}
		});
		this.context = this;
		btn_confirm = (Button) findViewById(R.id.btn_confirm);
		btn_confirm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final String uname = username.getText().toString();
				final String pwd = userpwd.getText().toString();
				final String uage = age.getText().toString();
				final String sex;
				if (checked) {
					sex = "男";
				} else {
					sex = "女";
				}
				if ("".equals(uname) || "".equals(pwd) || "".equals(uage)) {
					Toast.makeText(context, "请填写全部信息", Toast.LENGTH_SHORT).show();
				} else {
					new Thread() {
						public void run() {
							try {
								Socket socket = new Socket("192.168.43.243", 29999);
								Map<String, String> registerdata = new HashMap<String, String>();
								registerdata.put("用户名", uname);
								registerdata.put("密码", pwd);
								registerdata.put("性别", sex);
								registerdata.put("年龄", uage);
								JSONObject json = new JSONObject(registerdata);
								// 发送数据
								PrintWriter out = new PrintWriter(
										new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "utf-8")),
										true);
								out.println(json.toString());
								// 等待回应
								BufferedReader br = new BufferedReader(
										new InputStreamReader(socket.getInputStream(), "utf-8"));
								String mess = br.readLine();
								issuccess = mess;
								br.close();
								out.close();
								socket.close();

								if ("1".equals(mess)) {
									Message msg = new Message();
									handler.sendMessage(msg);
									finish();
								} else {
									Message msg = new Message();
									handler2.sendMessage(msg);

								}

							} catch (UnknownHostException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}

					}.start();
					handler = new Handler(new Handler.Callback() {// 这样写，就不弹出什么泄漏的警告了
						@Override
						public boolean handleMessage(Message msg) {

							Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();

							return false;
						}
					});
					handler2 = new Handler(new Handler.Callback() {// 这样写，就不弹出什么泄漏的警告了
						@Override
						public boolean handleMessage(Message msg) {

							Toast.makeText(getApplicationContext(), "已存在该用户！", Toast.LENGTH_SHORT).show();

							return false;
						}
					});
				}
			}
		});

		// female.setOnCheckedChangeListener(new OnCheckedChangeListener() {
		// @Override
		// public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
		// // TODO Auto-generated method stub
		// if (female.isSelected() == false) {
		// female.setSelected(true);
		// male.setSelected(false);
		// checked = true;    
		// } 
		// }
		// });
		// male.setOnCheckedChangeListener(new OnCheckedChangeListener() {
		// @Override
		// public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
		// // TODO Auto-generated method stub
		// if (male.isSelected() == false) {
		// male.setSelected(true);
		// female.setSelected(false);
		// checked = false;
		// }
		// }
		// });

	}

	// @Override
	// public void onClick(View v) {
	// // TODO Auto-generated method stub
	// switch (v.getId()) {
	// case R.id.btn_confirm:
	// final String uname = username.getText().toString();
	// final String pwd = userpwd.getText().toString();
	// final String uage = age.getText().toString();
	// final String sex;
	// if (checked){
	// sex = "男";
	// }
	// else{
	// sex = "女";
	// }
	// if ("".equals(uname) || "".equals(pwd) || "".equals(uage)) {
	// Toast.makeText(context, "请填写全部信息", Toast.LENGTH_SHORT)
	// .show();
	// } else {
	// new Thread() {
	// public void run() {
	// try {
	// Socket socket = new Socket("172.16.96.250", 29999);
	// Map<String, String> registerdata = new HashMap<String, String>();
	// registerdata.put("用户名", uname);
	// registerdata.put("密码", pwd);
	// registerdata.put("性别", sex);
	// registerdata.put("年龄", uage);
	// JSONObject json = new JSONObject(registerdata);
	// // 发送数据
	// PrintWriter pw = new PrintWriter(new BufferedWriter(
	// new OutputStreamWriter(socket.getOutputStream(),
	// "utf-8")), true);
	// pw.write(json.toString());
	// pw.flush();
	// // 等待回应
	// BufferedReader br = new BufferedReader(
	// new InputStreamReader(socket.getInputStream(),
	// "utf-8"));
	// String mess = br.readLine();
	// br.close();
	// pw.close();
	// socket.close();
	// if ("1".equals(mess)) {
	// finish();
	// }
	// if ("0".equals(mess)) {
	// Toast.makeText(context, "用户名已经存在，请重新填写",
	// Toast.LENGTH_SHORT).show();
	// username.setText("");
	// }
	// } catch (UnknownHostException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// } catch (IOException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// handler.sendEmptyMessage(0);// 执行完毕后给handler发送一个空消息
	// }
	//
	// // 定义Handler对象
	// private Handler handler = new Handler() {
	// @Override
	// // 当有消息发送出来的时候就执行Handler的这个方法
	// public void handleMessage(Message msg) {
	// super.handleMessage(msg);
	// // 处理UI
	// }
	// };
	// }.start();
	// }
	//// try {
	//// //判断用户输入状态
	////
	//// // 连接服务器
	//// Socket socket = new Socket("172.16.96.250", 45888);
	//// // 组装要传输的数据
	//// Map<String, String> registerdata = new HashMap<String, String>();
	//// registerdata.put("用户名", uname);
	//// registerdata.put("密码", pwd);
	//// registerdata.put("性别", sex);
	//// registerdata.put("年龄", uage);
	//// JSONObject json = new JSONObject(registerdata);
	//// // 发送数据
	//// PrintWriter pw = new PrintWriter(new BufferedWriter(
	//// new OutputStreamWriter(socket.getOutputStream(),
	//// "utf-8")), true);
	//// pw.write(json.toString());
	//// // 等待回应
	//// BufferedReader br = new BufferedReader(
	//// new InputStreamReader(socket.getInputStream(),
	//// "utf-8"));
	//// String mess = br.readLine();
	//// br.close();
	//// pw.close();
	//// socket.close();
	//// if ("1".equals(mess)) {
	//// finish();
	//// }
	//// if ("0".equals(mess)) {
	//// Toast.makeText(context, "用户名已经存在，请重新填写",
	//// Toast.LENGTH_SHORT).show();
	//// username.setText("");
	//// }
	//// } }
	//// catch (UnknownHostException e) {
	//// // TODO Auto-generated catch block
	//// e.printStackTrace();
	//// } catch (IOException e) {
	//// // TODO Auto-generated catch block
	//// e.printStackTrace();
	//// }
	// break;
	// default:
	// break;
	// }
	// }
}
